///////////////////////////////////////////////////////////
//  SaveItemCommand.cpp
//  Implementation of the Class SaveItemCommand
//  Created on:      21-Sep-2013 12:09:55 PM
//  Original author: Jakkaphan Aiemcharoenjit
///////////////////////////////////////////////////////////

#include "SaveItemCommand.h"
#include "CurrentCommand.h"

using namespace UI;
using namespace UI::CMD;

SaveItemCommand::SaveItemCommand(CCreateItemView* createItem, CCreateTemplateView* createTemplate)
{
  this->m_type = ICommand::SAVEITEM_CMD;
  this->m_createItemView = createItem;
  this->m_createTemplateView = createTemplate;
}


SaveItemCommand::~SaveItemCommand()
{

}


void SaveItemCommand::Execute()
{
  //IObject* curentData = this->m_createTemplateView->GetCurrentData();

  switch(CurrentCommand::GetInstance()->GetCurrentCommand())
  {
  case ICommand::EDITITEM_CMD:
    {
      std::string newName = this->m_createItemView->GetNameLabel();
      this->m_createTemplateView->EditCurrentNameData(newName);
      //std::string newName = this->m_createItemView->GetNameLabel();
      //switch(curentData->GetType())
      //{
      //case IObject::ITEM_T:
      //  {
      //    dynamic_cast<IItem*>(curentData)->SetName(newName);
      //  }
      //  break;
      //case IHeader::HEARDER_T:
      //  {
      //    dynamic_cast<IHeader*>(curentData)->SetName(newName);
      //  }
      //  break;
      //default: break;
      //}
    }
    break;
  case ICommand::NEWITEM_CMD:
    {
      IObject* newData = this->m_createItemView->CreateData();
      this->m_createTemplateView->AppendData(newData);
      /*if(curentData)
      {
        switch(curentData->GetType())
        {
        case IObject::ITEM_T:
          {
            IItem* item = dynamic_cast<IItem*>(curentData);
            switch(item->GetParent()->GetType())
            {
            case IObject::PAGE_T:
              {
                dynamic_cast<IPage*>(item->GetParent())->AppendChild(newData);
              }
              break;
            case IObject::HEARDER_T:
              {
                dynamic_cast<IHeader*>(item->GetParent())->AppendChild(newData);
              }
              break;
            default: break;
            }
          }
          break;
        case IHeader::HEARDER_T:
          {
            IHeader* header = dynamic_cast<IHeader*>(curentData);
          }
          break;
        default: break;
        }
      }
      else
        this->m_createTemplateView->GetPage()->AppendChild(newData);*/
    }
    break;
  case ICommand::INSERTITEM_CMD:
    {
      IObject* newData = this->m_createItemView->CreateData();
      this->m_createTemplateView->InsertData(newData);
    }
    break;
  default: break;
  }

  // Set current command
  this->m_createTemplateView->Refresh();
  CurrentCommand::GetInstance()->SetCurrentCommand(this->m_type);
}


ICommand::CommandType SaveItemCommand::GetType()
{
	return this->m_type;
}